<template>
  <div class="news">
    <div class="item" v-for="item in myNews">
      <img class="left" :src="item.bgimg" alt="新闻图片" />
      <div class="right">
        <div class="title">{{ item.title | subSentence(14) }}</div>
        <div class="desc">{{ item.desc | subSentence(15) }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "NewsItem",
  props: ["news"],
  data() {
    return {
      myNews: [],
    };
  },
  mounted() {
    setTimeout(() => {
      // 模拟异步获取数据
    }, 1000);
  },
  watch: {
    news(val) {
      this.myNews = val;
    },
  },
  filters: {
    // 第一个参数默认是过滤器左边的值
    subSentence(value, length) {
      return value.length > length ? value.substring(0, length) + "..." : value;
    },
  },
};
</script>

<style scoped>
.news {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.news .item {
  margin-bottom: 10px;
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.news .item img.left {
  width: 60px;
  height: 60px;
  border-radius: 5px;
  margin-right: 15px;
}

.news .item .right {
  flex: 1;
  text-align: left;
  height: 50px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: #2c3e50;
}

.news .item .right .title {
  font-size: 18px;
  font-weight: 520;
}

.news .item .right .desc {
  font-size: 14px;
}
</style>